﻿//  --------------------------------
//  Copyright (c) 2010 Michael Schuler, Sascha Burger. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://p2pmessaging.codeplex.com/license
//  ---------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using p2pMessaging.BL.Model;
using p2pMessaging.Interfaces.Contact;

namespace p2pMessaging.Tests
{
	[TestFixture]
	public class AddressBookTest
	{
		private AddressBook myAddressBook;
		
		[SetUp]
		public void SetUp()
		{
			myAddressBook = new AddressBook();
		}
		
		[Test]
		[ExpectedException(ExceptionType = typeof(NotImplementedException))]
		public void CopyTo()
		{
			myAddressBook.CopyTo(new IContact[0], 0);
		}
		
		[Test]
		public void Find()
		{
			fill();
			IList<IContact> result1 = myAddressBook.Find("Sch");
			IList<IContact> result2 = myAddressBook.Find("27a");
			IList<IContact> result3 = myAddressBook.Find("Burger");
			IList<IContact> result4 = myAddressBook.Find("8583");
			IList<IContact> result5 = myAddressBook.Find("Schu");
			
			Assert.AreEqual(3, result1.Count);
			Assert.AreEqual(1, result2.Count);
			Assert.AreEqual(1, result3.Count);
			Assert.AreEqual(1, result4.Count);
			Assert.AreEqual(2, result5.Count);
		}
		
		[Test]
		public void AddClearContainsCount()
		{
			Contact p1 = new Contact();
			Contact p2 = new Contact();
			Contact p3 = new Contact();
			Contact c1 = new Contact();
			Contact c2 = new Contact();
			Contact c3 = new Contact();
			
			p1.Children.Add(c1);
			p1.Children.Add(c2);
			p1.Children.Add(c3);
			p2.Children.Add(c1);
			p3.Children.Add(c2);
			
			myAddressBook.Add(p1);
			myAddressBook.Add(p2);
			myAddressBook.Add(p3);
			myAddressBook.Add(c1);
			myAddressBook.Add(c2);
			myAddressBook.Add(c3);
			
			Assert.AreEqual(6, myAddressBook.Count);
			Assert.IsTrue(myAddressBook.Contains(p1));
			Assert.IsTrue(myAddressBook.Contains(p2));
			Assert.IsTrue(myAddressBook.Contains(p3));
			Assert.IsTrue(myAddressBook.Contains(c1));
			Assert.IsTrue(myAddressBook.Contains(c2));
			Assert.IsTrue(myAddressBook.Contains(c3));

			bool result = myAddressBook.Remove(p1);

			Assert.IsTrue(result);
			Assert.AreEqual(5, myAddressBook.Count);
			Assert.IsFalse(myAddressBook.Contains(p1));

			result = myAddressBook.Remove(new Contact());
			Assert.AreEqual(5, myAddressBook.Count);
			
			Assert.IsFalse(result);

			myAddressBook.Clear();

			Assert.AreEqual(0, myAddressBook.Count);
			Assert.IsFalse(myAddressBook.Contains(p1));
			Assert.IsFalse(myAddressBook.Contains(p2));
			Assert.IsFalse(myAddressBook.Contains(p3));
			Assert.IsFalse(myAddressBook.Contains(c1));
			Assert.IsFalse(myAddressBook.Contains(c2));
			Assert.IsFalse(myAddressBook.Contains(c3));
			
			myAddressBook.Dispose();
		}
		
		[Test]
		public void IsReadOnly()
		{
			Assert.IsFalse(myAddressBook.IsReadOnly);
		}
		
		[Test]
		public void Enumerate()
		{
			Contact first = new Contact();
			myAddressBook.Add(first);
			
			fill();

			int count = 0;
			IEnumerator<IContact> enumerator = myAddressBook.GetEnumerator();
			while (enumerator.MoveNext())
			{
				Assert.IsNotNull(((IEnumerator)enumerator).Current);
				Assert.IsNotEmpty(enumerator.Current.Id);
				count++;
			}

			IEnumerator oldEnumerator = ((IEnumerable) myAddressBook).GetEnumerator();
			Assert.AreEqual(enumerator, oldEnumerator);

			Assert.AreEqual(7, count);

			myAddressBook.MoveNext();
			myAddressBook.MoveNext();
			
			myAddressBook.Reset();
			myAddressBook.MoveNext();
			
			Assert.AreEqual(first, myAddressBook.Current);
		}
		
		[Test]
		public void Constructor()
		{
			List<IContact> list = new List<IContact>();

			list.Add(new Contact());

			myAddressBook = new AddressBook(list);
			myAddressBook = new AddressBook(list.ToArray());
		}
		
		private void fill()
		{
			Contact p1 = new Contact { FirstName = "Michael", LastName = "Schuler", City = "Winterthur", ZipCode = "8404", EmailAddress = "mschuler@hsz-t.ch", Street = "Frauenfelderstrasse 27a" };
			Contact p2 = new Contact { FirstName = "Sascha", LastName = "Burger", City = "Sulgen", ZipCode = "8583", EmailAddress = "sburger@hsz-t.ch" };
			Contact p3 = new Contact { FirstName = "Michael", LastName = "Schuler", City = "Winterthur", ZipCode = "8409", EmailAddress = "mschuler@hsz-t.ch", Street = "Im oberen Gern 16" };
			Contact c1 = new Contact();
			Contact c2 = new Contact();
			Contact c3 = new Contact();

			p1.Children.Add(c1);
			p1.Children.Add(c2);
			p1.Children.Add(c3);
			p2.Children.Add(c1);
			p3.Children.Add(c2);

			myAddressBook.Add(p1);
			myAddressBook.Add(p2);
			myAddressBook.Add(p3);
			myAddressBook.Add(c1);
			myAddressBook.Add(c2);
			myAddressBook.Add(c3);
		}
	}
}
